<%@page import="cn.example.freid.util.ResultsetUtil"%>
<%@page import="cn.example.freid.domain.ExampleDomain"%>
<%@page import="java.util.List"%>
<%@page import="cn.example.freid.util.SQLStrings"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="cn.example.freid.util.JDBCUtil"%>
<%@page import="cn.example.freid.util.StringUtil"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%
	int pageSize = 12;
	int pageNo = 1;
	int totalPage = 0;
	
	String pageNoString = request.getParameter("pageNo");
	String pageSizeString = request.getParameter("pageSize");
	
	if (!StringUtil.isNullOrEmpty(pageSizeString))
		pageSize = Integer.parseInt(pageSizeString);
	
	if (!StringUtil.isNullOrEmpty(pageNoString))
		pageNo = Integer.parseInt(pageNoString);
	
	int startNo = (pageNo - 1) * pageSize;
	
	JDBCUtil jdbcUtil = null;
	jdbcUtil = new JDBCUtil();
	
	ResultSet rs = jdbcUtil.query(
	SQLStrings.QUERY_BYPAGE_SQL, startNo, pageSize);
	
	List<ExampleDomain> dataList = ResultsetUtil.resultsetToList(rs, ExampleDomain.class);
	
	rs = jdbcUtil.query(
			SQLStrings.QUERY_COUNT_SQL);
	rs.next();
	totalPage = (int)Math.ceil(rs.getInt(1)/(double)pageSize);
	jdbcUtil.closeConAndState();
%>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="fonts/favicon.ico">

    <title>Sign Admin</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="css/index.css" rel="stylesheet">

	<script>
		function doRemove(ID) {
			if (confirm("Confirm Delete?")) {
				$("#loadingImg").show();
				$.ajax({
	    			url : "./Delete.do" ,
	    			dataType : "json",
	    			type : "POST",
	    			data : {
	    				domainID : ID
	    			},
	    			cache : false,
	    			error : function(XMLHttpRequest, textStatus, errorThrown) {
	     				alert(XMLHttpRequest.responseText + errorThrown);
	    			},
	    			success : function(data) {
	    				if (data.error == 0) {
	    					$("#loadingImg").hide();
	    					location.reload();
	    				} else {
	    					$("#loadingImg").hide();
	    					alert(data.msg);
	    				}
	    			}
	    		});
	    		return false;
			}
    	}
	</script>

  </head>

  <body>

    <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container-fluid">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="#">Sign Admin</a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li><a href="#">Help</a></li>
          	<li><a href="./AdminLogOut.do">Log out</a></li>
          </ul>
        </div>
      </div>
    </nav>

    <div class="container-fluid">
      <div class="row">
        <div class="col-sm-3 col-md-2 sidebar">
          <ul class="nav nav-sidebar">
            <li class="active"><a href="#">Overview</a></li>
          </ul>
        </div>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Domains</h1>
          <div style="margin-bottom:20px" id="loadingImg">
          	  <img width="40" height="40" src="img/loading.gif">
              </div>
          <div class="table-responsive">
          	<button type="button" class="btn btn-xs btn-success" onClick="location.href='info.jsp'">Add New Domain</button>
          	<table class="table table-striped">
          		<thead>
                	<tr>
                		<th>ID</th>
                  		<th>Name</th>
                  		<th>Mail</th>
                  		<th>comments</th>
                  		<th>options</th>
                	</tr>
              	</thead>
              	<tbody>
              		<%
              			for (int i = 0; i < dataList.size(); i++) {
              		%>
              				<tr>
              					<td><%=dataList.get(i).getID() %></td>
              					<td><%=dataList.get(i).getName() %></td>
              					<td><%=dataList.get(i).getMail() %></td>
              					<td><%=dataList.get(i).getComments() %></td>
              					<td>
              						<button type="button" class="btn btn-xs btn-warning" 
              						onClick="location.href='info.jsp?id=<%=dataList.get(i).getID()%>'">Edit</button>
              						<button type="button" class="btn btn-xs btn-danger"
              						onClick="doRemove('<%=dataList.get(i).getID()%>')">Remove</button>
              					</td>
              				</tr>
              		<%		
              			}
              		%>
              	</tbody>
          	</table>
          	<%if (pageNo > 1) {%>
          	<button type="button" class="btn btn-xs btn-link" onclick="location.href='index.jsp?pageNo=<%= pageNo - 1 %>'"> &lt;-Previous</button>
          	<%} if (pageNo > 1 && pageNo < totalPage) {%>
          	|
          	<%} if (pageNo < totalPage) {%>
          	<button type="button" class="btn btn-xs btn-link" onclick="location.href='index.jsp?pageNo=<%= pageNo + 1 %>'"> Next-&gt; </button>
          	<%} %>
          </div>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="js/jquery-1.11.1.js"></script>
    <script src="js/bootstrap.js"></script>
    <script>
    	$("#loadingImg").hide();
    </script>
  </body>
</html>
